package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/20 10:51
 * description:
 */
public class AppHomeGoverAdapter extends RecyclerView.Adapter<AppHomeGoverAdapter.AppHomeGoverHolder> {

    private Context mContext;
    private List<QueryPageResourceRes.ResourceListBean> viewData;


    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header,int modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public AppHomeGoverAdapter(Context context) {
        mContext = context;
    }

    @Override
    public AppHomeGoverHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_gover_item, parent, false);
        return new AppHomeGoverHolder(view);
    }

    @Override
    public void onBindViewHolder(AppHomeGoverHolder holder, int position) {
        if (viewData != null) {
            final QueryPageResourceRes.ResourceListBean bean = viewData.get(position);

            if (viewData.get(position).getResource_url() != null) {
                Glide.with(mContext)
                        .load(viewData.get(position).getResource_url())
                        .into(holder.goverIv);
            }

            holder.goverIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = bean.getLink_url();
                    int need_header1 = bean.getNeed_header();
                    String sys_label1 = bean.getSys_label();
                    int modularId = bean.getModularId();
                    String access_token = ACache.get(mContext).getAsString("access_token");
                    if (access_token == null) {
                        JumpUtils.jump(mContext, "login", 0);
                    } else {
                        if (bean.getLink_url() == null) {
                            return;
                        } else {
                            if (baseInterface != null) {
                                baseInterface.onClick(v, url, sys_label1, need_header1,modularId);
                            }

                        }
//                            String sys_label = bean.getSys_label();
//                            /**
//                             * sys_label参数判断逻辑
//                             * 1.sys_label为null  或者sys_label 为"";不用管 直接跳转
//                             * 2.sys_label为NOHEADER 拼接access_token
//                             * 3.sys_label不为空，且不为NOHEADER，调张旺接口
//                             */
//                            L.e(Constant.TITLE, "url--" + url + "----syslabel---" + sys_label);
//                            if (url == null || url.trim().equals("")) {
//                                return;
//                            }
//                            Bundle bundle = new Bundle();
//                            int need_header = bean.getNeed_header();
//                            if (need_header == 1) {
//                                bundle.putString(Constant.TITLE, "");
//                            }
//                            if (sys_label != null && sys_label.equals("NOHEADER")) {
//                                bundle.putString("url", url + "?access_token=" + access_token);
//                                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
//                            } else if (sys_label != null && !sys_label.equals("NOHEADER")) {
//                                bundle.putString("url", url + "?access_token=" + access_token);
//                                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
//                            } else if (sys_label != null && sys_label.length() == 0) {
//                                bundle.putString("url", url);
//                                bundle.putString(Constant.TITLE, "");
//                                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
//                            } else if (sys_label == null) {
//                                bundle.putString("url", url);
//                                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
//                            }
//                        }
                    }

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(List<QueryPageResourceRes.ResourceListBean> viewData) {
        this.viewData = viewData;
    }

    public static class AppHomeGoverHolder extends RecyclerView.ViewHolder {

        private final ImageView goverIv;

        public AppHomeGoverHolder(View itemView) {
            super(itemView);
            goverIv = (ImageView) itemView.findViewById(R.id.apphome_gover_iv);
        }
    }

}
